﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering;
using MahApps.Metro.Controls;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// XmlEditor.xaml 的交互逻辑
    /// </summary>
    public partial class XmlEditor : MetroWindow
    {
        public XmlEditor()
        {
            InitializeComponent();
            this.Closing += XmlEditor_Closing;
            this.editorBase.TextChanged += EditorBase_TextChanged;
        }

        private void EditorBase_TextChanged(object sender, EventArgs e)
        {
            this.editorBase.IsModified = true;
            UpdateModifiedState();
        }

        private void UpdateModifiedState()
        {
            if (editorBase.IsModified)
            {
                tbStatus.Text = "已修改！";
            }
            else
            {
                tbStatus.Text = "";
            }
        }

        private void XmlEditor_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (editorBase.IsModified)
            {
                var answer = LMessageBox.Show("内容已修改，需要保存吗？", Globals.AppName, MessageBoxButton.YesNoCancel, MessageBoxImage.Question, "", null, this);
                if (answer == MessageBoxResult.Yes)
                {
                    SaveFile();
                    UpdateModifiedState();
                }
                else if (answer == MessageBoxResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }

        private string filePath;

        /// <summary>
        /// 载入xml 文件。
        /// </summary>
        /// <param name="fileName"></param>
        public void LoadXmlFile(string fileName)
        {
            filePath = fileName;
            editorBase.Load(fileName);
            editorBase.IsModified = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFile();
        }

        public void SaveFile()
        {
            try
            {
                System.IO.File.WriteAllText(filePath, editorBase.Text);
                editorBase.IsModified = false;
                UpdateModifiedState();
            }
            catch (Exception ex)
            {
                LMessageBox.ShowWarning(ex.Message);
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void editorBase_KeyDown(object sender, KeyEventArgs e)
        {
            L.GetKeyboardState(out bool isAlt, out bool isShift, out bool isCtrl);
            if (e.Key == Key.S && isCtrl)
            {
                SaveFile();
            }
        }
    }
}
